package com.fund.product.utils;

import com.fund.common.enums.ProductType;
import com.fund.product.constants.ProductConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

public class ProductCodeGenerator {

    StringRedisTemplate stringRedisTemplate;

    public ProductCodeGenerator(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public String generateProductCode(ProductType productType) {
        //取得存储的前缀（如：product:generator:1 ,此处1用来区分不同类型的序列号）
        String prefix = ProductConstants.PRODUCT_GENERATOR_PREFIX + productType.getValue().toString();
        //取得序列号并且进行拼接
        Long serialNumber = stringRedisTemplate.opsForValue().increment(prefix);
        String serialString = String.format("%05d", serialNumber);
        return productType.getValue() + serialString;

    }
}
